package com.ymt.bpm.service.engine.bpd.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.model.Const.Oplog;
import com.ymt.bpm.model.PBpd;
import com.ymt.bpm.model.PBpdAuth;
import com.ymt.bpm.service.engine.bpd.BpdAuthService;
import com.ymt.bpm.service.platform.log.OpLogService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.DhxUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/6.
 */
@Service
public class BpdAuthServiceImpl implements BpdAuthService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private String mapper = "PBpdAuthMapper";

    @Autowired
    private MDao mDao;

    @Autowired
    private OpLogService oplog;

    @Override
    public JSONObject getAuth(String tenantId, int offset,
                             int limit,
                             String bpdId, String orgId, String authType, String memberType) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(Const.TENANT_ID, tenantId);
        paramMap.put("bpdId", bpdId);
        paramMap.put("authType", authType);
        paramMap.put("memberType", memberType);
        PageHelper.offsetPage(offset, limit);
        List<PBpdAuth> list = mDao.selectList(mapper, "query", paramMap);
        PageInfo pageInfo = new PageInfo(list);
        JSONObject rtn = new JSONObject();
        JSONArray rows = new JSONArray();
        if (list!=null && list.size()>0) {
            for (PBpdAuth bpd : list) {
                JSONObject row = DhxUtil.toFastJsonWithDate(bpd, Const.LANG_ZH, -480);
                row.put("_ITEM_NO", ++offset);
                rows.add(row);
            }
        }
        rtn.put("rows", rows);
        rtn.put("total", pageInfo.getTotal());
        return rtn;
    }

    @Override
    public JSONObject addAuth(String tenantId,
                              String loginName,
                              String displayName,
                              String bpdId,
                              String orgId, String authType,
                              String memberType,
                              JSONArray members) {
        PBpdAuth auth = new PBpdAuth();
        auth.setTenantId(tenantId);
        auth.setBpdId(bpdId);
        auth.setAuthType(authType);
        auth.setMemberType(memberType);
        int rs = 0;
        if (members!=null && members.size()>0) {
            for (int i=0; i<members.size(); i++) {
                JSONObject member = members.getJSONObject(i);
                auth.setMemberId(member.getString("memberId"));
                auth.setMemberName(member.getString("memberName"));
                //check existing
                PBpdAuth old = mDao.selectOne(mapper, "selectByObj", auth);
                if (old!=null) {
                    continue;   //skip existing
                }
                rs += mDao.insert(mapper, "insert", auth);
                auth.setMemberId(null);
                auth.setMemberName(null);
            }
        }
        JSONObject rtn = new JSONObject();
        rtn.put("result", rs);

        //记录日志
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(Const.TENANT_ID, tenantId);
        paramMap.put("bpdId", bpdId);
        PBpd bpd = mDao.selectOne(mapper, "selectByPrimaryKey", paramMap);
        oplog.bizlog(tenantId, loginName, displayName, null, Oplog.OP.UPDATE, Oplog.CODE.PROCESS_BPD_AUTH, bpd.getBpdName());

        return rtn;
    }

    @Override
    public JSONObject delAuth(String tenantId,
                              String loginName,
                              String displayName,
                              String bpdId,
                                 String orgId, String authType,
                                 String memberType, String memberId) {
        PBpdAuth auth = new PBpdAuth();
        auth.setTenantId(tenantId);
        auth.setBpdId(bpdId);
        auth.setAuthType(authType);
        auth.setMemberType(memberType);
        auth.setMemberId(memberId);
        int rs = mDao.delete(mapper, "deleteByObj", auth);
        JSONObject rtn = new JSONObject();
        rtn.put("result", rs);

        //记录日志
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(Const.TENANT_ID, tenantId);
        paramMap.put("bpdId", bpdId);
        PBpd bpd = mDao.selectOne(mapper, "selectByPrimaryKey", paramMap);
        oplog.bizlog(tenantId, loginName, displayName, null, Oplog.OP.DELETE, Oplog.CODE.PROCESS_BPD_AUTH, bpd.getBpdName());

        return rtn;
    }

}
